package com.lecing.fetch.util;

import com.lecing.data.entity.Community;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 抓取数据
 */
public class FetchApp {
    /**
     * 解析页面并获取小区相关字符串
     * @param content 页面源代码字符串
     * @return 小区相关字符串列表
     * @throws ParseException
     * @throws IOException
     */
    public static List<String> parserHtml(String content) throws ParseException, IOException {
        Document doc = Jsoup.parse(content);
        String pageNo=doc.getElementsByClass("curr").text();
        System.out.println("当前页："+pageNo);

        //1,7,13,19,25,31
        Elements  eles=doc.select("p.bot-tag").select("a[href]");
        Elements  addrEles=doc.select("div.li-info").select("address");

        int commIndex=0;
        List<String> hrefList=new ArrayList<>();
        for(int i=1;i<eles.size();i=i+6){
            Element element=eles.get(i);
            String hrefv=element.attr("href");

            StringBuilder sb=new StringBuilder();
            sb.append(hrefv).append("&").append(addrEles.get(commIndex).text());
            //System.out.println("获取小区信息字符串:"+sb.toString());
            commIndex++;

            //添加到list容器
            hrefList.add(sb.toString());
        }
        return  hrefList;
    }


    /**
     * 取得html页面源码
     * @param url
     * @return
     * @throws Exception
     */
    public static String getHtmlByUrl(String url) throws Exception{

        //设置代理
        System.getProperties().setProperty("http.proxyHost", "61.135.217.7");
        System.getProperties().setProperty("http.proxyPort", "80");

        CloseableHttpClient httpClient= HttpClients.createDefault(); // 创建httpClient实例
        // 访问的目标站点，端口和协议
        HttpHost targetHost = new HttpHost("www.java2000.net");
        // 代理的设置
        HttpHost proxy = new HttpHost("61.135.217.7", 80);
        RequestConfig requestConfig = RequestConfig.custom().build();
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        HttpGet httpGet=new HttpGet(url); // 创建httpget实例

        httpGet.setHeader("Accept", "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpGet.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
        httpGet.setHeader("Accept-Encoding", "gzip, deflate");
        httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
        httpGet.setHeader("Connection", "keep-alive");
       // httpGet.setHeader("Host", url);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"); // 设置请求头消息User-Agent

        //  httpGet.setHeaders(); 设置多个请求头
        CloseableHttpResponse response=httpClient.execute(httpGet); // 执行http get请求
        HttpEntity entity=response.getEntity(); // 获取返回实体
        String htmlCode= EntityUtils.toString(entity, "utf-8");// 获取网页内容

        response.close(); // response关闭
        httpClient.close(); // httpClient关闭

        return htmlCode;
    }
    /**
    public  static void main(String[] args) throws  Exception{
        String url="http://xa.anjuke.com/community/p4";
        String pageCode=getHtmlByUrl(url);
        System.out.print("pagecode:"+pageCode);
        parserHtml(pageCode);
    }
     */
}
